package com.copyctrl.campus.portal.controller;

import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.constant.HttpConstant;
import com.copyctrl.campus.common.constant.RedisConstant;
import com.copyctrl.campus.common.domain.User;
import com.copyctrl.campus.common.model.ApiResponse;
import com.copyctrl.campus.portal.service.QuestionService;
import com.copyctrl.model.CampComment;
import com.copyctrl.model.CampFound;
import com.copyctrl.model.CampQuestion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author:JH
 * @version:1.0
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    QuestionService service;

    @Autowired
    StringRedisTemplate redisTemplate;

    @PostMapping("/{question}")
    public ApiResponse add(@PathVariable String question, HttpServletRequest request) {
        String openid = request.getHeader(HttpConstant.USER);
        //从请求头中获取openid,转换成id存入redis中
        String userJson = redisTemplate.opsForValue().get(RedisConstant.USER_KEY + openid);
        User user = JSONUtil.toBean(userJson, User.class);
        Integer id=user.getId();
        CampQuestion campQuestion = JSONUtil.toBean(question, CampQuestion.class);
        campQuestion.setUid(id);
        campQuestion.setTime(new Date());
        int i=service.add(campQuestion);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("发布失败请联系管理员");
    }
    @DeleteMapping("/{id}")
    public ApiResponse delete(@PathVariable Integer id) {
        int i=service.delete(id);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("发布失败请联系管理员");
    }
    @PutMapping("/{question}")
    public ApiResponse update(@PathVariable String question) {
        CampQuestion campQuestion = JSONUtil.toBean(question, CampQuestion.class);
        int i=service.update(campQuestion);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("发布失败请联系管理员");
    }

    @GetMapping("/all")
    public ApiResponse findAll(){
        return ApiResponse.ofSuccess(service.find());
    }
    @GetMapping("/user/{id}")
    public ApiResponse findByUid(@PathVariable Integer id){
        return ApiResponse.ofSuccess(service.findByUid(id));
    }

    @GetMapping("/{id}")
    public ApiResponse findById(@PathVariable Integer id){
        return ApiResponse.ofSuccess(service.findById(id));
    }

    @PostMapping("/comment/{comment}")
    public ApiResponse comment(@PathVariable String comment,HttpServletRequest request){
        String openid = request.getHeader(HttpConstant.USER);
        CampComment campComment = JSONUtil.toBean(comment, CampComment.class);
        campComment.setUser(openid);
        int i = service.saveComment(campComment);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("发布失败请联系管理员");
    }
    @DeleteMapping("/comment/{id}")
    public ApiResponse deleteCommnet(@PathVariable Integer id) {
        int i=service.deleteComment(id);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("发布失败请联系管理员");
    }
}
